package com.pumpkin.web.busi.controller;

import com.pumpkin.web.model.UserModel;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (description)
 *
 * @author jinlong.liao on 2016/6/30
 */
@Controller
public class HomeController extends BaseController {

    @ModelAttribute("name")
    public String initName(@RequestParam(required = false) String name) {
        return name == null ? "姚明" : name;
    }

    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/usingTexts", method = RequestMethod.GET)
    public String usingTexts(@RequestParam String name, ModelMap modelMap) {
        modelMap.addAttribute("name", name);
        return "using_texts";
    }

    @RequestMapping(value = "/expressionSyntax", method = RequestMethod.GET)
    public String expressionSyntax(@RequestParam String name, @RequestParam(required = false) Integer age, ModelMap modelMap) {
        UserModel userModel = new UserModel();
        userModel.setId(1);
        userModel.setName("廖金龙");
        userModel.setAge(28);
        userModel.setAdmin(false);

        modelMap.addAttribute("name", name);
        modelMap.addAttribute("age", age);
        modelMap.addAttribute("userModel", userModel);
        modelMap.addAttribute("names", new String[]{"廖金龙", "刘荣丽", "纠结哥", "贤内助"});

        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.format(new Date());

        modelMap.addAttribute("dateFormat", dateFormat);

        return "expression_syntax";
    }
}
